package com.ezcolour.ticket.common.factory;

import com.ezcolour.ticket.common.cache.Cache;
import com.ezcolour.ticket.common.cache.CacheKey;
import com.ezcolour.ticket.dao.RoleDao;
import com.ezcolour.ticket.dao.UserDao;
import com.ezcolour.ticket.model.Role;
import com.ezcolour.ticket.util.SpringContextHolder;
import com.ezcolour.ticket.util.ToolUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Component
@DependsOn("springContextHolder")
public class ConstantFactory implements IConstantFactory  {

    private RoleDao roleDao = SpringContextHolder.getBean(RoleDao.class);
    private UserDao userDao = SpringContextHolder.getBean(UserDao.class);


    public static IConstantFactory me() {
        return SpringContextHolder.getBean("constantFactory");
    }
    /**
     * 通过角色id获取角色名称
     */
    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_NAME + "'+#roleId")
    public String getSingleRoleName(Long roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role roleObj = roleDao.findByRoleId(roleId);
        if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
            return roleObj.getName();
        }
        return "";
    }

    /**
     * 通过角色id获取角色英文名称
     */
    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_TIP + "'+#roleId")
    public String getSingleRoleTip(Long roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role roleObj = roleDao.findByRoleId(roleId);
        if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
            return roleObj.getTips();
        }
        return "";
    }

}
